package com.zntg.eureka_service_h2;

/**
 * @program: springclould_ylcaifu_App
 * @description:
 * @author: zhengyunfei
 * @create: 2019-01-15 11:19
 **/

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlToConvert {

    public static void main(String args[]) {
        String html = "<h2>ABCfD</h2>gsdggas<h3>woff</h3>jkll<h2>005</h2>";
        String htmlArray[]=html.split("</h2>");
        List strList = new ArrayList();
        for(int i=0;i<htmlArray.length;i++){
            strList.add(htmlArray[i]);
        }
        // 简单示例，相当于String html=getHtml(String urlString);
        List resultList = getContext(html);
        for(int i=0;i<strList.size();i++){
                    html=html.replace("<h2>"+resultList.get(i)+"</h2>","<h2 id=\""+resultList.get(i)+"\">"+resultList.get(i)+"</h2>");
        }
       /* for (Iterator iterator = resultList.iterator(); iterator.hasNext();) {
            String context = (String) iterator.next();
           // System.out.println(context);
        }*/
       System.out.println(html);
       // getSinaIP();
    }
    public static String getContentConvert(String html){
        String result="";
        String htmlArray[]=html.split("</h2>");
        List strList = new ArrayList();
        for(int i=0;i<htmlArray.length;i++){
            strList.add(htmlArray[i]);
        }
        // 简单示例，相当于String html=getHtml(String urlString);
        List<PostDictoryBo> resultList = getContextObj(html);
        for(int i=0;i<strList.size();i++){
            html=html.replace("<h2>"+resultList.get(i)+"</h2>","<h2 id=\""+resultList.get(i)+"\">"+resultList.get(i)+"</h2>");
        }
        return result;
    }

    public static List getContext(String html,String label) {
        List resultList = new ArrayList();
        Pattern p = Pattern.compile("<"+label+">([^</"+label+">]*)");// 匹配<title>开头，</title>结尾的文档
        Matcher m = p.matcher(html);// 开始编译
        while (m.find()) {
            resultList.add(m.group(1));// 获取被匹配的部分
        }
        return resultList;
    }

    public static List getContext(String html) {
        List resultList = new ArrayList();
        Pattern p = Pattern.compile("<h2>([^</h2>]*)");// 匹配<title>开头，</title>结尾的文档
        Matcher m = p.matcher(html);// 开始编译
        while (m.find()) {
            resultList.add(m.group(1));// 获取被匹配的部分
        }
        return resultList;
    }
    public static List<PostDictoryBo> getContextObj(String html) {
        List<PostDictoryBo> resultList = new ArrayList();
        Pattern p = Pattern.compile("<h2>([^</h2>]*)");// 匹配<title>开头，</title>结尾的文档
        Matcher m = p.matcher(html);// 开始编译
        while (m.find()) {
            PostDictoryBo postDictoryBo=new PostDictoryBo();
            postDictoryBo.setName(m.group(1));
            resultList.add(postDictoryBo);// 获取被匹配的部分
        }
        return resultList;
    }
    /**
     * @param html要解析的html文档内容
     * @return 解析结果，可以多次匹配，每次匹配的结果按文档中出现的先后顺序添加进结果List
     */
    public static List getContexts(String html) {
        List resultList = new ArrayList();
        // <[^<|^>]*>
        // >*<
        // >([^<]*)
        // <title>([^</title>]*)
        // new Array\\(([^\\);]*)
        Pattern p = Pattern.compile("new Array\\(([^\\);]*)");
        Matcher m = p.matcher(html);// 开始编译
        while (m.find()) {
            int gc = m.groupCount();
            String group = m.group(1);
            if (!"".equals(group)) {
                System.out.println(gc + group);
                resultList.add(group);
            }
        }
        return resultList;
    }

}